package com.zhdl.modules.demo.dglProcess.Robot;

import cn.hutool.core.util.HexUtil;
import com.project.common.util.DelayUtil;
import com.zhdl.common.ytools.BaseConversionTools;
import com.zhdl.hardware_contact.hardware_common.param.CurrencyProtocolReadReq;
import com.zhdl.hardware_contact.hardware_common.param.CurrencyProtocolWriteReq;
import com.zhdl.hardware_contact.hardware_common.param.TcpAssembleCmdPkg;
import com.zhdl.hardware_contact.hardware_config.entity.ProjectConfiguration;
import com.zhdl.modules.demo.dglProcess.Robot.RobotChildProcess;
import com.zhdl.modules.demo.dglProcess.Robot.RobotRunService;
import com.zhdl.modules.process.service.BaseConfigValueProvider;
import com.zhdl.network.strategy.common_client.impl.CommonProtocolStrategy;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;

@Service
@Slf4j
public class WorkPlace1RobotInterface extends BaseConfigValueProvider implements RobotRunService {
    private float childProcessFlag = 0.0f;
    public final String ROBOT_RUNNING = "robot_running";
    public final String ROBOT_DONE = "robot_done";
    private final int robotBoolStart = 264;
    private final int robotFloatStart = 267;
    @Resource
    ThreadPoolTaskScheduler threadPoolTaskScheduler;
    @Resource
    CommonProtocolStrategy commonProtocolStrategy;
    public final static String ROBOT_IP = "127.0.0.1:502";
    private int stepCount = 0;

    @Override
    public void initData() {
        configurationPLcList = new ArrayList<>();
        configurationPLcList.add(new ProjectConfiguration(ROBOT_RUNNING, "0", 1));
        configurationPLcList.add(new ProjectConfiguration(ROBOT_DONE, "0", 1));
        threadPoolTaskScheduler.scheduleWithFixedDelay(() -> readRobotData(),500);
    }
    private void readRobotData(){
       // sendReadCmd(20,40);
    }

    @Override
    public void startChildProcess(RobotChildProcess childProcess) {
        getValue(ROBOT_DONE).setValue("0");
        childProcessFlag = childProcess.getType();
        log.info("机器人发送数据"+childProcess);
        sendWriteCmd(robotFloatStart,BaseConversionTools.floatTo8hexHL(childProcessFlag));
        sendWriteCmd(robotBoolStart+2,"FFFF");
        DelayUtil.delayedExecution(100, () -> sendWriteCmd(robotBoolStart+2,"0000"));
        stepCount = 0;
        DelayUtil.delayedExecution(2000, new Runnable() {
            @Override
            public void run() {
                getValue(ROBOT_DONE).setValue("1");
            }
        });
    }

    public void robotNextStep(){
        getValue(ROBOT_DONE).setValue("0");
        stepCount++;
        sendWriteCmd(robotBoolStart,stepCount%2==0?"FFFF0000":"0000FFFF");
        DelayUtil.delayedExecution(2000, new Runnable() {
            @Override
            public void run() {
                getValue(ROBOT_DONE).setValue("1");
            }
        });
    }

    private void sendReadCmd(int address,int count){
        CurrencyProtocolReadReq currencyProtocolReadReq = new CurrencyProtocolReadReq();
        currencyProtocolReadReq.setId("01");
        currencyProtocolReadReq.setReadNumber(BaseConversionTools.intToShortHex(address));
        currencyProtocolReadReq.setStartAddress(BaseConversionTools.intToShortHex(count));
        try {
            commonProtocolStrategy.sendCmd(ROBOT_IP,HexUtil.encodeHexStr(TcpAssembleCmdPkg.read03(currencyProtocolReadReq)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    private void sendWriteCmd(int address,String data){
        CurrencyProtocolWriteReq currencyProtocolReadReq1 = new CurrencyProtocolWriteReq();
        currencyProtocolReadReq1.setId("01");
        currencyProtocolReadReq1.setStartAddress(BaseConversionTools.intToShortHex(address));
        currencyProtocolReadReq1.setWriteValue(data);
        try {
            commonProtocolStrategy.sendCmd(ROBOT_IP,HexUtil.encodeHexStr(TcpAssembleCmdPkg.write10(currencyProtocolReadReq1)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public enum ChildProcess{
        INIT(1,"机器人初始化"), PICK_UP(2,"导电棒取料上方");
        private int type;
        private String desc;
        ChildProcess(int type, String desc) {
            this.type = type;
            this.desc = desc;
        }

        public int getType() {
            return type;
        }

        public void setType(int type) {
            this.type = type;
        }

        public String getDesc() {
            return desc;
        }

        public void setDesc(String desc) {
            this.desc = desc;
        }
    }

}
